/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.cadastro;

import br.com.saber.certificacao.beansjsf.interfacebean.BeanJSFInterface;
import br.com.saber.certificacao.beansjsf.login.LoginManager;
import br.com.saber.certificacao.beansjsf.semescopo.SelectItemManager;
import br.com.saber.certificacao.ejbs.stateless.interfaces.QuestaoBeanLocal;
import br.com.saber.certificacao.modelos.Questao;
import br.com.saber.certificacao.observador.ControleObserver;
import br.com.saber.certificacao.utils.UtilMetodos;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author gbvbahia
 */
@ManagedBean(name = "questaoBloqueioManager")
@ViewScoped
public class QuestaoBloqueioManager implements BeanJSFInterface, Observer {

    @EJB
    private QuestaoBeanLocal questaoBean;

    @ManagedProperty("#{loginManager}")
    private LoginManager loginManager;

    @ManagedProperty("#{selectItemManager}")
    private SelectItemManager selectItemManager;

    private List<Questao> questoesModel;

    /** Creates a new instance of QuestaoBloqueioManager */
    public QuestaoBloqueioManager() {
    }

    //====================
    // Iniciadores
    //====================
    @Override
    @PostConstruct
    public void init() {
        ControleObserver.addBeanObserver(loginManager.getUsuario(), this);
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "QuestaoBloqueioManager.init() executado!");
    }

    @Override
    @PreDestroy
    public void end() {
        ControleObserver.removeBeanObserver(loginManager.getUsuario(), this);
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "QuestaoBloqueioManager.end() executado!");
    }
    //====================
    //Métodos de Negócio
    //====================

    @Override
    public void update(Observable o, Object arg) {
        String[] args = (String[]) arg;
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals(ControleObserver.Eventos.CAD_QUESTAO_EVENT) || args[i].equals(ControleObserver.Eventos.BLOQ_QUESTAO_EVENT)) {
                atualizarModel();
            }
        }
    }

    public void atualizarModel() {
        List<Questao> listAux = new ArrayList<Questao>(questaoBean.buscarQuestoesPorUsuario(this.loginManager.getUsuario(), false, false));
        this.questoesModel = new ArrayList<Questao>(listAux);
    }

    //====================
    //Table Actions
    //====================
    public void alterarQuestao(Questao questao) {
        if (questaoBean.alterarStatusQuestao(questao)) {
            ControleObserver.notificaObservers(this.loginManager.getUsuario(), new String[]{ControleObserver.Eventos.BLOQ_QUESTAO_EVENT});
            UtilMetodos.messageFactoringFull("questaoBloqueadaOK", FacesMessage.SEVERITY_INFO, FacesContext.getCurrentInstance());
        } else {
            UtilMetodos.messageFactoringFull(questao.getId().toString(), null, "questaoBloqueadaNOK", FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
        }
    }
    //====================
    //SelectItem
    //====================
    //=========================
    //Getters AND Setters
    //=========================

    public LoginManager getLoginManager() {
        return loginManager;
    }

    public void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }

    public SelectItemManager getSelectItemManager() {
        return selectItemManager;
    }

    public void setSelectItemManager(SelectItemManager selectItemManager) {
        this.selectItemManager = selectItemManager;
    }

    public List<Questao> getQuestoesModel() {
        if (this.questoesModel == null) {
            atualizarModel();
        }
        return questoesModel;
    }

    public void setQuestoesModel(List<Questao> questoesModel) {
        this.questoesModel = questoesModel;
    }
}
